// 递归遍历  路由表数据 然后新增一些属性
function routerTree(params: any) {
  const routeList: any = [];
  params.forEach(
    (element: {
      path: any;
      name: any;
      redirect: any;
      hidden: any;
      hideChildrenInMenu: any;
      component: string | string[];
      meta: { title: any; icon: any; hidden: any };
      children: string | any[];
    }) =>
      routeList.push({
        path: element.path,
        name: element.name,
        redirect: element.redirect,
        hidden: element.hidden,
        hideChildrenInMenu: !!element.hideChildrenInMenu,
        // component:
        //   element.component.indexOf("/") === -1
        //     ? // 判断是布局页面还是业务页面
        //       /* @vite-ignore */
        //       () => import(`@comp/layouts/${element.component}`)
        //     : /* @vite-ignore */
        //       () => import(`@view/${element.component}`),
        meta: {
          title: element.meta.title,
          icon: element.meta.icon ? element.meta.icon : "fff",
          hidden: !!element.meta.hidden,
          keepAlive: true,
        },
        children:
          element.children && element.children.length
            ? routerTree(element.children)
            : null,
      })
  );
  return routeList;
}

// 给basiclayout的数据
function createMenuRouter(params: any) {
  const routeList: any = [];
  params.forEach(
    (element: {
      meta: { title: any };
      hidden: any;
      parentId: any;
      children: string | any[];
    }) => {
      routeList.push({
        title: element.meta.title,
        hidden: element.hidden,
        parentId: element.parentId,
        child:
          element.children && element.children.length
            ? layoutRouterItem(element.children)
            : null,
        meta: element.meta,
      });
    }
  );
  return routeList;
}

/**
 * @description: 获取路由name
 * @description: 如果路由跳转是path而不是name，那么由path推断出name，
 * @description: 要求name和path不能随意取值（name是path最后一级路由）
 * @param {type}
 * @return:
 */
function getPathName(to: { name: any; path: string }) {
  let name;
  if (to.name) {
    name = to.name;
  } else if (to.path) {
    let pathArr = to.path.split("/");
    name = pathArr[pathArr.length - 1];
  }
  return name;
}

// 递归路由表 生成符合a-menu组件的数据结构 也就是菜单列表
function layoutRouterItem(params: any) {
  const arrItem: any = [];
  params.forEach(
    (element: {
      path: any;
      name: any;
      hidden: any;
      parentId: any;
      hideChildrenInMenu: any;
      meta: { title: any; icon: any; target: any };
      children: string | any[];
    }) =>
      arrItem.push({
        path: element.path,
        name: element.name,
        hidden: element.hidden,
        parentId: element.parentId,
        hideChildrenInMenu: !!element.hideChildrenInMenu,
        meta: {
          title: element.meta.title,
          icon: element.meta.icon ? element.meta.icon : "fff",
          target: element.meta.target ? element.meta.target : null,
          keepAlive: true,
        },
        children:
          element.children && element.children.length
            ? layoutRouterItem(element.children)
            : null,
      })
  );
  return arrItem;
}

export { routerTree, createMenuRouter, layoutRouterItem, getPathName };
